package com.kaishengit.web;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kaishengit.entity.Person;
import com.kaishengit.service.PersonService;

public class XmlServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		PersonService service = new PersonService();
		List<Person> list = service.findAll();
		
		
		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		out.print("<persons>");
		
		for(Person p : list) {
			out.print("<person id=\""+p.getId()+"\">");
			out.print("<name>"+p.getName()+"</name>");
			out.print("<address>" + p.getAddress() + "</address>");
			out.print("</person>");
		}
		
		out.print("</persons>");
		
		out.flush();
		out.close();
		
	}
}
